package com.umeox.watch.moto.launcher.fragment;


import static com.umeox.watch.moto.launcher.event.DeviceStatusChangedEvent.UNBOUND;
import static com.umeox.watch.moto.launcher.ui.FragmentFactory.getIdleFragments;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import com.umeox.watch.moto.launcher.R;
import com.umeox.watch.moto.launcher.adapter.HomePagerAdapter;
import com.umeox.watch.moto.launcher.event.DeviceStatusChangedEvent;
import com.umeox.watch.moto.launcher.event.ScrollToPagerEvent;
import com.umeox.watch.moto.launcher.ui.MainActivity;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;


/**
 * Created by zl on 2017/10/23.
 */

public class IdleFragment extends Fragment {

    //    private static final String TAG = IdleFragment.class.getSimpleName();
    private static final String TAG = "ThemeFragment";
    private static final int QUICK_SETTING_PAGE_INDEX = 0;
    private static final int HOME_PAGE_INDEX = 1;
    private static final int NOTIFICATION_INDEX = 2;

    private MainActivity parent;
    HomePagerAdapter mPagerAdapter;
    private static IdleFragment INSTANCE;
    ViewPager2 mPager;
    private boolean isBind;
    private View mView;

    public IdleFragment() {
        INSTANCE = this;
    }

    public static IdleFragment getInstance() {
        if (INSTANCE == null) {
            INSTANCE = new IdleFragment();
        }
        return INSTANCE;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        parent = (MainActivity) context;
        EventBus.getDefault().register(this);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }


    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.frag_idle, container, false);
        return mView;
    }


    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mPager = mView.findViewById(R.id.vpager);
        mPagerAdapter = new HomePagerAdapter(this);
        mPager.setOffscreenPageLimit(2);
        mPager.setAdapter(mPagerAdapter);

        mPagerAdapter.setFragments(getIdleFragments());
        mPager.setCurrentItem(HOME_PAGE_INDEX,false);
        parent.setIdleFragmentIsHome(parent.idleFragmentIsHome());
        mPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                switch (position) {
                    case QUICK_SETTING_PAGE_INDEX:
                    case NOTIFICATION_INDEX:
                        parent.setPagingEnabled(false);
                        parent.setIdleFragmentIsHome(false);
                        break;
                    case HOME_PAGE_INDEX:
                        parent.setPagingEnabled(true);
                        parent.setIdleFragmentIsHome(true);
                        break;
                }
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onDeviceStatusChangedEvent(DeviceStatusChangedEvent event) {
        if (event.mDeviceStatus == UNBOUND) {
            refreshUnbindUI();
            scrollToPager(HOME_PAGE_INDEX, true);
            return;
        }

        refreshBindUI();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onBackPressedEvent(ScrollToPagerEvent event) {
        scrollToPager(event.pagerIndex, true);
        if(event.pagerIndex == HOME_PAGE_INDEX){
            parent.setIdleFragmentIsHome(true);
            parent.setPagingEnabled(true);
        }
    }

    /**
     * 跳转指定页面
     *
     * @param index        界面索引
     * @param smoothScroll true平滑滚动 false 直接跳转
     */
    public void scrollToPager(int index, boolean smoothScroll) {
        if (mPager != null) {
            mPager.setCurrentItem(index, smoothScroll);
        }
    }

    /**
     * 设备被解绑时,刷新界面
     */
    public void refreshUnbindUI() {

        if (mPagerAdapter != null) {
            mPagerAdapter.setFragments(getIdleFragments());
            mPagerAdapter.notifyDataSetChanged();
        }
    }

    /**
     * 设备被解绑时,刷新界面
     */
    public void refreshBindUI() {
        if (mPagerAdapter != null) {
            mPagerAdapter.setFragments(getIdleFragments());
            mPagerAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        EventBus.getDefault().unregister(this);
    }


}
